package com.party.controller.user.download;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.Download;
import com.party.model.SysUser;
import com.party.service.download.DownloadService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "download")
public class DownloadController {

    @Resource
    DownloadService downloadService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest req) {
        ModelAndView modelAndView = new ModelAndView();
        String type = req.getParameter("type");
        String url="/download/query.do?type="+type;
        modelAndView.addObject("url",url);
        modelAndView.setViewName("/user/download/index");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String type = req.getParameter("type");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        Download download = new Download();
        download.setTitle(title);
        download.setType(type);
        Page p = this.downloadService.getDownloadList(download, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "content")
    public ModelAndView content(
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        Download download = new Download();
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Integer id = Integer.parseInt(idStr);
            download = this.downloadService.getDownloadByPrimaryKey(id);
        }
        modelAndView.addObject("obj",download);
        byte[]binaryContent=download.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);
        modelAndView.setViewName("user/download/content");
        return modelAndView;
    }
}
